package com.hxzy.servlet;

import com.hxzy.service.HandSetService;
import com.hxzy.service.impl.HandSetServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Objects;

/**
 * @author nick
 */
@WebServlet("/remove")
public class RemoveServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String hsId = req.getParameter("hsId");
        if (Objects.nonNull(hsId)) {
            //调用业务逻辑层
            HandSetService service = new HandSetServiceImpl();
            int remove = service.remove(Integer.valueOf(hsId));

            String result = remove > 0 ? "删除成功" : "删除失败";

            resp.setCharacterEncoding("utf-8");
            resp.setContentType("text/html;charset=utf-8");

            PrintWriter writer = resp.getWriter();
            writer.println("<script>");
            writer.println("alert('"+result+"')"); //alert会阻塞页面，只有点击了确定后才会执行后续的代码
            writer.println("window.location.href='index'"); //点击确定后，再次回到首页
            writer.println("</script>");
            writer.flush();
            writer.close();
        } else { //没有传参数时，重定向到起始页
            resp.sendRedirect("index");
        }
    }
}
